package LecturaXML;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
 
import java.net.URL;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;

import java.net.MalformedURLException;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import Consultas.Usuario;
 
public class UsuarioParserSax
{
    private URL UsuarioUrl;
 
    public UsuarioParserSax(String url)
    {
        try
        {
            this.UsuarioUrl = new URL(url);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    } 
 
    public List<Usuario> parse()
    {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        try
        {
            SAXParser parser = factory.newSAXParser();
            UsuarioHandler handler = new UsuarioHandler();
            parser.parse(this.getInputStream(), handler);
            return handler.getUsuarios();
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }
 
    private InputStream getInputStream()
    {
        try
        {  
            return UsuarioUrl.openConnection().getInputStream();
        }
        catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }
}